home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / indx18eu.zip / XCHANGE.ASM < prev    next >
Assembly Source File  |  1991-03-14  |  4KB  |  90 lines

  1. %TITLE "Exchange two variables proc"
  2.  
  3.                 .MODEL  TPascal
  4.  
  5.                 .CODE
  6.  
  7.                 PUBLIC  Exchange
  8.  
  9. ;-----------------------------------------------------------------------;
  10. ;--                                                                   --;
  11. ;-- PURPOSE :    This assembly PROC will exchange the contents of any --;
  12. ;--              two variables.  This routine was copied out of the   --;
  13. ;--              Turbo Assembler User's Guide with changes to make    --;
  14. ;--              it follow the TPascal MODEL.                         --;
  15. ;--                                                                   --;
  16. ;--              THIS CODE IS NOT MINE and all creative credit        --;
  17. ;--              belongs to Borland Personnel.                        --;
  18. ;--                                                                   --;
  19. ;-- AUTHOR  :  Borland International, Inc.                            --;
  20. ;--                                                                   --;
  21. ;-----------------------------------------------------------------------;
  22.  
  23. %NEWPAGE
  24.  
  25. ;---------------------------------------------------------------;
  26. ;--  PROCEDURE Exchange ( VAR var1               ;            --;
  27. ;--                       VAR var2               ;            --;
  28. ;--                           count              : WORD ) ;   --;
  29. ;--                                                           --;
  30. ;---------------------------------------------------------------;
  31. ;--                                                           --;
  32. ;--   INPUT :                                                 --;
  33. ;--             var1    :  POINTER                            --;
  34. ;--                        - any VAR name                     --;
  35. ;--             var2    :  POINTER                            --;
  36. ;--                        - any VAR name                     --;
  37. ;--             count   :  WORD                               --;
  38. ;--                        -  a non-zero value indicating the --;
  39. ;--                           number of bytes to Exchange     --;
  40. ;--                                                           --;
  41. ;---------------------------------------------------------------;
  42. ;--                                                           --;
  43. ;--  EXAMPLE CALL FROM PASCAL:                                --;
  44. ;--                                                           --;
  45. ;--  Exchange ( rec1 , rec2 , SizeOf ( RecType ) ) ;          --;
  46. ;--                                                           --;
  47. ;---------------------------------------------------------------;
  48.  
  49. Exchange  PROC FAR var1:DWORD,var2:DWORD,count:WORD
  50.  
  51.                 LOCALS  @@
  52.  
  53.                 cld                        ;exchange goes upward
  54.  
  55.                 mov     dx,ds              ;save DS
  56.  
  57.                 lds     si,var1            ;get first address
  58.                 les     di,var2            ;get second address
  59.  
  60.                 mov     cx,count           ;get number of bytes to move
  61.  
  62.                 shr     cx,1               ;get word count (low bit -> carry)
  63.                 jnc     ExchangeWords      ;if no odd byte, enter loop
  64.  
  65.                 mov     al,es:[di]         ;read odd byte from var2
  66.                 movsb                      ;move a byte from var1 to var2
  67.                 mov     [si-1],al          ;write var2 byte to var1
  68.  
  69.                 jz      Finis              ;done if only one byte to exchange
  70.  
  71. ExchangeWords:
  72.                 mov     bx,-2              ;BX is a handy place to keep -2
  73.  
  74. ExchangeLoop:
  75.                 mov     ax,es:[di]         ;read a word from var2
  76.                 movsw                      ;do a move from var1 to var2
  77.                 mov     [bx][si],ax        ;write var2 word to var1
  78.                 loop    ExchangeLoop       ;repeat "count div 2" times
  79.  
  80. Finis:
  81.                 mov     ds,dx              ;get back Turbo's DS
  82.  
  83.                 ret
  84.  
  85. ENDP            Exchange
  86.  
  87. ENDS            CODE
  88.  
  89.                 END                        ; END OF SOURCE
  90.